package com.sky.controller.admin;

import cn.hutool.db.Page;
import com.sky.dto.PageDTO;
import com.sky.dto.SetmealDTO;
import com.sky.query.SetmealQuery;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    @GetMapping("/page")
    @ApiOperation("分页查询套餐接口")
    public Result<PageDTO<SetmealVO>> queryPage(SetmealQuery setmealQuery){
        log.info("分页查询条件：{}",setmealQuery);
        PageDTO<SetmealVO> pageDTO = setmealService.queryPage(setmealQuery);
        return Result.success(pageDTO);
    }

    @PostMapping
    @ApiOperation("新增套餐接口")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐：{}",setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售、停售接口")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status,@RequestParam("id")Long id){
        log.info("套餐起售、停售：{}",id);
        setmealService.updateStatus(status,id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改套餐接口")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result UpdateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐：{}",setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询套餐接口")
    public Result<SetmealVO> queryById(@PathVariable Long id){
        log.info("根据ID查询套餐：{}",id);
        SetmealVO setmealVO = setmealService.queryById(id);
        return Result.success(setmealVO);
    }

    @DeleteMapping
    @ApiOperation("根据ID删除套餐接口")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("根据ID删除套餐：{}",ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }

}
